home *** CD-ROM | disk | FTP | other *** search
/ Oh!X 2000 Spring / Oh!X 2000 Spring Special CD-ROM (Japan) (Part 2).7z / Oh!X 2000 Spring Special CD-ROM (Japan) (Part 2).bin / DXF / samples / multimedia / dplay / src / duel / diutil.cpp < prev    next >
C/C++ Source or Header  |  1999-03-09  |  5KB  |  164 lines

  1. //-----------------------------------------------------------------------------
  2. // File: DIUtil.cpp
  3. //
  4. // Desc: Input routines
  5. //
  6. // Copyright (C) 1995-1999 Microsoft Corporation. All Rights Reserved.
  7. //-----------------------------------------------------------------------------
  8. #include "duel.h"
  9. #include "diutil.h"
  10. #include "gameproc.h"
  11.  
  12.  
  13. //-----------------------------------------------------------------------------
  14. // Globals
  15. //-----------------------------------------------------------------------------
  16. static LPDIRECTINPUT       g_pDI;               // DirectInput interface
  17. static LPDIRECTINPUTDEVICE g_pdidKeyboard;      // Keyboard device interface
  18. static BOOL                g_bKeyboardAcquired; // Whether eyboard is acquired
  19.  
  20.  
  21.  
  22.  
  23. //-----------------------------------------------------------------------------
  24. // Name: DIUtil_InitInput()
  25. // Desc: Initialize DirectInput objects & devices
  26. //-----------------------------------------------------------------------------
  27. HRESULT DIUtil_InitInput( HWND hWnd )
  28. {
  29.     // Create DI object
  30.     if( FAILED( DirectInputCreate( (HINSTANCE)GetWindowLong( hWnd, GWL_HINSTANCE ),
  31.                                    DIRECTINPUT_VERSION, &g_pDI, NULL ) ) )
  32.     {
  33.         ShowError(IDS_DINPUT_ERROR_DIC);
  34.         return E_FAIL;
  35.     }
  36.  
  37.     // Create keyboard device
  38.     if( FAILED( g_pDI->CreateDevice( GUID_SysKeyboard, &g_pdidKeyboard, NULL ) ) )
  39.     {
  40.         ShowError(IDS_DINPUT_ERROR_CD);
  41.         return E_FAIL;
  42.     }
  43.  
  44.     // Tell DirectInput that we want to receive data in keyboard format
  45.     if( FAILED( g_pdidKeyboard->SetDataFormat( &c_dfDIKeyboard) ) )
  46.     {
  47.         ShowError(IDS_DINPUT_ERROR_DF);
  48.         return E_FAIL;
  49.     }
  50.  
  51.     // Set cooperative level
  52.     if( FAILED( g_pdidKeyboard->SetCooperativeLevel( hWnd,
  53.                                   DISCL_NONEXCLUSIVE | DISCL_FOREGROUND ) ) )
  54.     {
  55.         ShowError(IDS_DINPUT_ERROR_SP);
  56.         return E_FAIL;
  57.     }
  58.  
  59.     // try to acquire the keyboard
  60.     if( SUCCEEDED( g_pdidKeyboard->Acquire() ) )
  61.         g_bKeyboardAcquired = TRUE;
  62.     else
  63.         g_bKeyboardAcquired = FALSE;
  64.  
  65.     return S_OK;
  66. }
  67.  
  68.  
  69.  
  70.  
  71. //-----------------------------------------------------------------------------
  72. // Name: DIUtil_ReadKeys()
  73. // Desc: Use DirectInput to read game-play keys
  74. //-----------------------------------------------------------------------------
  75. VOID DIUtil_ReadKeys( DWORD* pdwKeys )
  76. {
  77.     BYTE    rgbKeybd[256];
  78.     DWORD   dwKeys = 0L;
  79.     HRESULT hr;
  80.  
  81.     hr = g_pdidKeyboard->GetDeviceState( sizeof(rgbKeybd), rgbKeybd );
  82.     if( FAILED(hr) )
  83.     {
  84.         if( hr == DIERR_INPUTLOST )
  85.         {
  86.             // We lost control of the keyboard, reacquire
  87.             if( SUCCEEDED( g_pdidKeyboard->Acquire() ) )
  88.                 g_bKeyboardAcquired = TRUE;
  89.             else
  90.                 g_bKeyboardAcquired = FALSE;
  91.         }
  92.  
  93.         // Failed to read the keyboard, just return
  94.         return;
  95.     }
  96.  
  97.     // check & update key states
  98.     if( rgbKeybd[DIK_NUMPAD5] & 0x80 )
  99.         dwKeys |= KEY_STOP;
  100.  
  101.     if( (rgbKeybd[DIK_NUMPAD2] & 0x80) || (rgbKeybd[DIK_DOWN] & 0x80) )
  102.         dwKeys |= KEY_DOWN;
  103.  
  104.     if( (rgbKeybd[DIK_NUMPAD4] & 0x80) || (rgbKeybd[DIK_LEFT] & 0x80) )
  105.         dwKeys |= KEY_LEFT;
  106.  
  107.     if( (rgbKeybd[DIK_NUMPAD6] & 0x80) || (rgbKeybd[DIK_RIGHT] & 0x80) )
  108.         dwKeys |= KEY_RIGHT;
  109.  
  110.     if( (rgbKeybd[DIK_NUMPAD8] & 0x80) || (rgbKeybd[DIK_UP] & 0x80) )
  111.         dwKeys |= KEY_UP;
  112.  
  113.     if( rgbKeybd[DIK_SPACE] & 0x80 )
  114.         dwKeys |= KEY_FIRE;
  115.  
  116.     // Return the keys
  117.     (*pdwKeys) = dwKeys;
  118. }
  119.  
  120.  
  121.  
  122.  
  123. //-----------------------------------------------------------------------------
  124. // Name: DIUtil_CleanupInput()
  125. // Desc: Cleans up DirectInput objects
  126. //-----------------------------------------------------------------------------
  127. VOID DIUtil_CleanupInput()
  128. {
  129.     if(g_bKeyboardAcquired)
  130.     {
  131.         g_pdidKeyboard->Unacquire();
  132.         g_bKeyboardAcquired = FALSE;
  133.     }
  134.  
  135.     if( g_pdidKeyboard )
  136.         g_pdidKeyboard->Release();
  137.  
  138.     if( g_pDI )
  139.         g_pDI->Release();
  140. }
  141.  
  142.  
  143.  
  144.  
  145. //-----------------------------------------------------------------------------
  146. // Name: DIUtil_ReacquireInputDevices()
  147. // Desc: Reacquires DirectInput devices as needed
  148. //-----------------------------------------------------------------------------
  149. HRESULT DIUtil_ReacquireInputDevices()
  150. {
  151.     g_bKeyboardAcquired = FALSE;
  152.  
  153.     if( NULL == g_pdidKeyboard )
  154.         return E_FAIL;
  155.  
  156.     g_pdidKeyboard->Acquire();
  157.     g_bKeyboardAcquired = TRUE;
  158.     
  159.     return S_OK;
  160. }
  161.  
  162.  
  163.  
  164.